package com.xing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.DTO.ConferenceUserDTO;
import com.xing.dao.ConferenceDao;
import com.xing.dao.ConferenceUserDao;
import com.xing.entity.ConferenceEntity;
import com.xing.entity.ConferenceUserEntity;
import com.xing.service.ConferenceUserService;
import com.xing.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2024/2/19 1:16
 */
@Service
public class ConferenceUserServiceImpl
        extends ServiceImpl<ConferenceUserDao, ConferenceUserEntity> implements ConferenceUserService {
    @Autowired
    private ConferenceUserDao dao;
    @Autowired
    private ConferenceDao conferenceDao;


    public QueryWrapper<ConferenceUserEntity> getParams(Map<String, Object> params) {
        QueryWrapper<ConferenceUserEntity> queryWrapper = new QueryWrapper<>();
        String name = (String) params.get("params");
        queryWrapper.like(!StringUtils.isEmpty(name), "conference_number", name);
        queryWrapper.eq("del_flag", 0);
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        queryWrapper.eq("user_id", principal.getId());
        queryWrapper.orderByDesc("time");
        return queryWrapper;
    }


    @Override
    public IPage Page(Map<String, Object> params) {
        QueryWrapper<ConferenceUserEntity> params1 = getParams(params);
        int pageNum = Integer.parseInt(params.get("page").toString());
        int limit = Integer.parseInt(params.get("limit").toString());
        Page<ConferenceUserEntity> conferenceUserEntityPage = new Page<>(pageNum, limit);
        Page<ConferenceUserEntity> page = page(conferenceUserEntityPage, params1);
        Page<ConferenceEntity> page1 = new Page<>();
        page1.setCurrent(page.getCurrent());
        page1.setPages(page.getPages());
        page1.setSize(page.getSize());
        page1.setTotal(page.getTotal());
        List<ConferenceEntity> list = new ArrayList<>();
        for (ConferenceUserEntity record : page.getRecords()) {
            ConferenceEntity conferenceEntity = conferenceDao.selectById(record.getConferenceId());
            list.add(conferenceEntity);
        }
        page1.setRecords(list);
        return page1;
    }

    @Override
    public String create(ConferenceUserDTO dto) {
        return null;
    }

    @Override
    public void Delete(String id) {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        QueryWrapper<ConferenceUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("conference_id", id);
        queryWrapper.eq("user_id", principal.getId());
        queryWrapper.eq("del_flag", 0);
        ConferenceUserEntity conferenceUserEntity = dao.selectOne(queryWrapper);
        conferenceUserEntity.setDel_flag(1);
        updateById(conferenceUserEntity);
    }

    @Override
    public void laeve(ConferenceUserDTO dto) {
        try {
            ConferenceUserEntity entity = dao.selectById(dto.getId());
            entity.setLeaveDate(new Date());
            dao.updateById(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ConferenceUserDTO> getRecord(String code) {
        List<ConferenceUserDTO> conferenceUserDTOS = dao.GetDto(code);
        return conferenceUserDTOS;
    }
}
